#include "C_SpaceShip.h"


C_SpaceShip::C_SpaceShip() : m_evMousePress(S_MousePressMove::makep())
{
	m_scale = 60.0f;

	m_addBit = nullptr;
}


C_SpaceShip::~C_SpaceShip()
{
}

void C_SpaceShip::handleEvent(sf::Event e)
{
	VC_Drawable::handleEvent(e);
	switch (e.type)
	{
	case sf::Event::MouseWheelMoved:
	{
									   if (e.mouseWheel.delta > 0)
									   {
										   m_scale += 0.5f;
									   }
									   else
									   {
										   m_scale -= 0.5f;
									   }
									   FOR_VEC_I(it, m_bits, C_BitShip*)
									   {
										   (*it)->updateData();
									   }
									   if (m_addBit != nullptr)
									   {
										   m_addBit->updateData();
									   }
	}
	case sf::Event::MouseButtonReleased:
	{
										m_evMousePress->onRelease();

										   if (e.mouseButton.button == sf::Mouse::Button::Left)
										   {
											   if (m_addBit != nullptr)
											   {
												   if (m_addBit->tempState() == false) break;
												   m_bits.push_back(m_addBit);
												   m_addBit->setTemp(false);
												   m_addBit = nullptr;
											   }
										   }

										   break;
	}

	case sf::Event::MouseButtonPressed:
	{
										  if (e.mouseButton.button == sf::Mouse::Button::Right)
										  {
											  m_evMousePress->onPress(e.mouseButton.x, e.mouseButton.y);
										  }
										  break;
	}
	case sf::Event::MouseMoved:
	{
								  if (m_evMousePress->isPress)
								  {
									  m_evMousePress->move(e.mouseMove.x, e.mouseMove.y);
									  m_pos.x += m_evMousePress->x;
									  m_pos.y += m_evMousePress->y;

								  }
								  break;
	}
	}
	FOR_VEC_I(it, m_bits, C_BitShip*)
	{
		(*it)->handleEvent(e);
	}
}

void C_SpaceShip::render()
{
	glPushMatrix();
		glTranslatef(m_pos.x, m_pos.y, 0.0f);
		FOR_VEC_I(it, m_bits, C_BitShip*)
		{
			glPushMatrix();
			glTranslatef((*it)->shipPos().x*m_scale, (*it)->shipPos().y*m_scale, 0.0f);
				(*it)->render();
			glPopMatrix();
		}

		//render adding element
		if (m_addBit != nullptr)
		{
			glPushMatrix();
			glTranslatef(m_addBit->shipPos().x*m_scale, m_addBit->shipPos().y*m_scale, 0.0f);
			m_addBit->render();
			glPopMatrix();
		}
	glPopMatrix();
}

void C_SpaceShip::update()
{
	FOR_VEC_I(it, m_bits, C_BitShip*)
	{
		(*it)->update();
	}

	if (m_addBit != nullptr)
	{
		float x = mouseX() - m_pos.x;
		float y = mouseY() - m_pos.y;

		int xi = floor(x / m_scale);
		int yi = floor(y / m_scale);
		m_addBit->setShipPos(xi, yi);
		m_addBit->setTempState(this->testCoordPlace(xi, yi));

	}
}

void C_SpaceShip::addBit(C_BitShip* bs, int x, int y)
{
	bs->bind(this);
	bs->setShipPos(x, y);
	m_bits.push_back(bs);
}

void C_SpaceShip::addBit(C_BitShip* bs)
{
	bs->bind(this);
	bs->setShipPos(1, 1);
	m_addBit = bs;
}

bool C_SpaceShip::testCoordPlace(int x, int y)
{
	bool res = false;
	FOR_VEC_I(it, m_bits, C_BitShip*)
	{
		S_Pointi p = (*it)->shipPos();
		if (
			(p.x == x - 1 || p.x == x + 1) &&
			(p.y == y || p.y == y)
			)
		{
			res = true;
		}
		if (
			(p.x == x || p.x == x) &&
			(p.y == y - 1 || p.y == y + 1)
			)
		{
			res = true;
		}
		//critical
		if (p.x == x && p.y == y) return false;
	}

	return res;
}

C_BitShip* C_SpaceShip::find(int x, int y)
{
	FOR_VEC_I(it, m_bits, C_BitShip*)
	{
		if ((*it)->shipPos().x == x && (*it)->shipPos().y == y)
		{
			return (*it);
		}
	}
	return nullptr;
}

void C_SpaceShip::removeBit(C_BitShip* bs)
{
	FOR_VEC_I(it, m_bits, C_BitShip*)
	{
		if ((*it) == bs)
		{

		}
	}
}
